﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Newtonsoft.Json;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model.CommonObject.FengQiaoAPIEntity;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net;
using System.IO;

namespace ZBJF.Warranty.FengQiao
{
    public class FengQiaoHelper
    {
       public void SFOrder(){ 
           string xml = "<Request service='OrderService' lang='zh-CN'><Head>BSPdevelop</Head><Body><Order orderid='GW148999595994' express_type='1' j_province='广东省' j_city='深圳市' j_company='顺丰速运' j_contact='喵小萌' j_tel='95338' j_address='广东省深圳市福田区新洲十一街' d_province='北京' d_city='北京' d_company='一家公司' d_contact='萌小喵' d_tel='18888998899' d_address='北京市海淀区科学园科健路908' parcel_quantity='1' pay_method='1' custid='7551878519' ><Cargo name='服装' count='1' unit='台' weight='2.36' amount='2000' currency='CNY' source_area='中国'></Cargo></Order></Body></Request>";
           string Checkword = "j8DzkIFgmlomPt0aLuwU"; string verifyCode =MD5ToBase64String(xml+Checkword );
           string requestUrl = "http://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService "; //测试环境地址 
           //"https://bsp-ois.sit.sf-express.com:9443/bsp-ois/sfexpressService";//开发环境地址 //
           string result =DoPost(requestUrl, xml, verifyCode);//这就得到了返回结果，解析部分就不记了，想起来也没什么小点了 
       }
        public string MD5ToBase64String(string str) { 
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] MD5 = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); //MD5(注意UTF8编码)   
            string result = Convert.ToBase64String(MD5);//Base64 
            return result; 
        } 
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {
            // 总是接受 
            return true; 
        } 
        public string DoPost(string Url, string xml, string verifyCode) { 
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 
            string postData = string.Format("xml={0}&verifyCode={1}",xml, verifyCode); //请求
            WebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST"; 
            request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; 
            request.ContentLength = Encoding.UTF8.GetByteCount(postData);
            byte[] postByte = Encoding.UTF8.GetBytes(postData); 
            Stream reqStream = request.GetRequestStream(); 
            reqStream.Write(postByte, 0, postByte.Length); reqStream.Close(); //读取
            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
            Stream myResponseStream = response.GetResponseStream(); 
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close(); 
            return retString;
        }

    }
}
